<template>
  <!-- 邀请记录 -->
  <div class="app-container">
    <div class="formDiv">
      <el-form ref="form" :model="form" :inline="true" label-width="150px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="邀请人昵称">
              <el-input v-model="form.uid_realname" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="邀请人手机号">
              <el-input v-model="form.uid_phone" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="邀请人身份">
              <el-select v-model="form.uid_role_id" placeholder="请选择" clearable>
                <el-option
                  v-for="(item,index) in invitedType"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="被邀请人昵称">
              <el-input v-model="form.user_realname" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="被邀请人手机号">
              <el-input v-model="form.user_phone" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="邀请关系">
              <el-select v-model="form.source" placeholder="请选择" clearable>
                <el-option
                  v-for="(item,index) in invitedRoles"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="邀请时间" width="236">
              <el-date-picker
                v-model="form.time"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button type="primary" @click="handleSearch">查询</el-button>
              <el-button type="primary" @click="handleExport">导出</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="listDiv" v-loading="tableLoading">
      <el-table ref="tableData" :data="tableData" style="width: 100%">
        <!--:index="table_index"  -->
        <el-table-column
          fixed
          type="index"
          label="序号"
          :index="table_index"
          width="50"
          align="center"
        />
        <el-table-column label="活动名称" prop="activity_name" align="center" />
        <el-table-column label="邀请人昵称" prop="uid_name" align="center" />
        <el-table-column label="邀请人手机号" prop="uid_phone" align="center" />
        <el-table-column label="邀请人身份" prop="uid_role_name" align="center" />
        <el-table-column label="被邀请人昵称" prop="user_name" align="center" />
        <el-table-column label="被邀请人手机号" prop="user_phone" align="center" />
        <el-table-column label="邀请关系" prop="invite_relationship" align="center" />
        <el-table-column label="邀请时间" prop="created_at" align="center" width="200px">
          <template slot-scope="scope">{{ scope.row.created_at | dates }}</template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form.page"
            :page-sizes="page_sizes"
            :page-size="form.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { inviteRecord, inviteExport } from "@/api/invitationMange";
import commonFn from "@/utils/common";
export default {
  name: "invitedRecord",
  data() {
    return {
      form: {
        market_activity_id: "", //活动id
        uid_realname: "",
        uid_phone: "",
        uid_role_id: "",
        user_realname: "",
        user_phone: "",
        source: "",
        time: "",
        page: 1,
        pagesize: 10
      },
      tableData: [],
      invitedType: [
        { label: "普通用户", value: 0 },
        { label: "plus会员", value: 1 },
        { label: "服务商", value: 2 }
      ],
      invitedRoles: [
        { label: "普通用户邀请", value: 0 },
        { label: "plus会员邀请", value: 1 },
        { label: "服务商邀请", value: 2 }
      ],
      tableLoading: true,
      page_sizes: [10, 30, 50, 100], // 每页显示个数
      page_totals: 0
    };
  },
  filters: {
    // 时间戳转化
    dates(e) {
      if (e != 0) {
        return new Date(Number(e) * 1000).format("yyyy-MM-dd hh:mm:ss");
      } else {
        return "";
      }
    }
  },
  created() {
    this.getList(this.form);
    this.form.market_activity_id = this.$route.query.id;
    // console.log("id", this.form.market_activity_id);
  },
  methods: {
    // 序列码
    table_index(index) {
      return (this.form.page - 1) * this.form.pagesize + index + 1;
    },
    getList(opt) {
      inviteRecord(opt)
        .then(res => {
          if (res.data.code == 200) {
            this.tableData = res.data.data;
            this.page_totals = res.data.page_totals;
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
          this.tableLoading = false;
        })
        .catch(err => {
          this.errorStatus(err.msg);
        });
    },
    // 每页显示条数
    handleSizeChange(val) {
      this.form.pagesize = val;
      this.getList(this.form);
    },
    // 当前页
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form);
    },
    // 查询
    handleSearch() {
      this.form.page = 1;
      this.getList(this.form);
    },
    // 导出
    handleExport() {
      let data = { ...this.form };
      delete data.page;
      delete data.pagesize;
      inviteExport(data)
        .then(res => {
          commonFn.downFile("邀请记录导出", res);
        })
        .catch(err => {
          this.errorStatus(err.msg);
        });
    },
    // 弹窗组件
    eleMsg(txt, status) {
      this.$message({
        showClose: true,
        message: txt,
        type: status
      });
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.eleMsg(txt, "error");
    }
  }
};
</script>
<style scoped>
.el-range-editor--medium.el-input__inner {
  width: 236px;
}
.pagesCon {
  text-align: right;
  margin-top: 20px;
}
</style>